--
-- Author: shunguo.chen
-- Date: 2018-03-19 14:49:49
--
-- ChessHeroView	谜棋影响选择
--

local ChessSelectHeroRender = import(".renders.ChessSelectHeroRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.ChessHeroScene

local REMAIN_TIME_STR = L("left_time_sec")

function ClassRef:init()
	self._ui.Button_queding:addClickEventListener(function()
			local info = self._curRender:getData()
			executeFunc(self._onOperate, "selectHero", info.selectID)
			executeFunc(self._onCloseView, self)
		end)
	self._ui.Button_quxiao:addClickEventListener(function()
			self:_unselectRender()
		end)

	local renders = {0,0,0}
	for i=1,3 do
		local node = self._ui["Panel_" .. i]
		local render = ChessSelectHeroRender.create(node)
		render:setTag(i)
		render:onBtnClick(handler(self, self._onClickRender))
		renders[i] = render
	end
	self._renders = renders

	-- 选中／取消选中 需要移动的距离
	self._moveDis = renders[2]:getPositionX() - renders[1]:getPositionX()
end

-- data = ChessSelect
-- info = ChessInfo
function ClassRef:refresh(data, info)
	self._data = data
	self._info = info

	local infos = data.infos
	for i,render in ipairs(self._renders) do
		render:setData(infos[i])
	end

	self:_refreshTime()
end

function ClassRef:_onClickRender(render, op)
	local info = render:getData()
	if op == "openRole" then
		executeFunc(self._onOperate, "openRole", info.data)
	elseif op == "selectRole" then
		self:_selectRender(render)
	end
end

function ClassRef:_selectRender(render)
	if self._curRender then 
		display.pushToast("selectRender in action")
		return 
	end

	self._ui.tips:setVisible(false)

	self._curRender = render
	render:setSelected(true)
	local tag = render:getTag()
	local acts = {}
	for i,v in ipairs(self._renders) do
		if i ~= tag then
			local act = cca.seq({cca.fadeOut(0.1), cca.hide()})
			acts[#acts + 1] = cca.targeted(act, v)
		end
	end
	if tag ~= 2 then
		local dis = tag < 2 and self._moveDis or -self._moveDis
		acts[#acts + 1] = cca.targeted(cca.moveBy(0.2, dis, 0), render)
	end

	local scale1 = cca.scaleTo(0.15, 1.1)
	local scale2 = cca.scaleTo(0.15, 1)
	local func = cca.cb(function()
			self._ui.Button_queding:setVisible(true)
			self._ui.Button_quxiao:setVisible(true)
		end)
	local action = cca.seq({cca.spawn(acts), scale1, scale2, func})
	render:runAction(action)
end

function ClassRef:_unselectRender()
	if not self._curRender then return end

	self._ui.Button_queding:setVisible(false)
	self._ui.Button_quxiao:setVisible(false)

	local render = self._curRender
	local tag = render:getTag()
	render:setSelected(false)

	local acts = {}
	if tag ~= 2 then
		local dis = tag < 2 and -self._moveDis or self._moveDis
		acts[#acts + 1] = cca.targeted(cca.moveBy(0.2, dis, 0), render)

		for i,v in ipairs(self._renders) do
			if i ~= tag then
				local act = cca.seq({cca.delay(0.1), cca.show(), cca.fadeIn(0.1)})
				acts[#acts + 1] = cca.targeted(act, v)
			end
		end
	else
		for i,v in ipairs(self._renders) do
			if i ~= tag then
				local act = cca.seq({cca.show(), cca.fadeIn(0.1)})
				acts[#acts + 1] = cca.targeted(act, v)
			end
		end
	end

	local func = cca.cb(function()
		self._curRender = nil
		self._ui.tips:setVisible(true)
		self._ui.tips:setString(string.format(L(REMAIN_TIME_STR), self._info:getRemainTime()))
	end)

	local action = cca.seq({cca.spawn(acts), func})
	render:runAction(action)
end

function ClassRef:_refreshTime()
	local remainTime = self._info:getRemainTime()
	if remainTime <= 0 then
		display.pushToast(L("lua_code_text_251"))
		executeFunc(self._onCloseView, self)
		return
	end

	if self._ui.tips:isVisible() then
		self._ui.tips:setString(string.format(L(REMAIN_TIME_STR), remainTime))
	end
	self._ui.tips:performWithDelay(function()
			self:_refreshTime()
		end, 1)
end

return ClassRef
